﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Tss.MagicWeb.Domain.Abstract;
using Tss.MagicWeb.Domain.Entities;

namespace Tss.MagicWeb.Domain.Concrete
{
    public class DeckItemRepository : IDeckItemRepository, IDisposable
    {
        private MagicWebInterfaceEntities context = new MagicWebInterfaceEntities();

        public DeckItem Read(long id)
        {
            return context.DeckItems.FirstOrDefault(di => di.Id == id);
        }

        public IQueryable<DeckItem> Query()
        {
            return context.DeckItems;
        }

        public IQueryable<DeckItem> Query(Expression<Func<DeckItem, bool>> filter)
        {
            return context.DeckItems.Where(filter);
        }

        public void Create(DeckItem entity)
        {
            context.AddToDeckItems(entity);
            context.SaveChanges();
        }

        public void Delete(DeckItem entity)
        {
            context.DeckItems.DeleteObject(entity);
            context.SaveChanges();
        }

        public void Update(DeckItem entity)
        {
            context.SaveChanges();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool cleanManaged)
        {
            if (cleanManaged)
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }
    }
}
